#include "widget.h"
#include "ui_widget.h"
#include <QTimerEvent>
#include <QTimer>
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
//根据启动器的id不同方便查找具体定时器
    timeId1 = startTimer(1000); //参数1 间隔，单位毫秒
    timeId2 = startTimer(2000); //参数1 间隔，单位毫秒
//创建定时器对象
    QTimer *timer = new QTimer(this);
//启动定时器
    timer->start(500);
//定时器每过500ms就会发送一个信号
    connect(timer, &QTimer::timeout, [ = ]() {
        static int num = 1;
        ui->label_3->setText(QString::number(num++));
    });
    connect(ui->pushButton, &QPushButton::clicked, [ = ]() {
        if(timer->isActive()) {
            timer->stop();
        } else {
            timer->start();
        }
    });
}
Widget::~Widget()
{
    delete ui;
}
void Widget::timerEvent(QTimerEvent *event)
{
    if(event->timerId() == timeId1) {
        static int num1 = 1;
        ui->label->setText(QString::number(num1++));
    }
    if(event->timerId() == timeId2) {
        static int num2 = 1;
        ui->label_2->setText(QString::number(num2++));
    }
}
